// FBWF_GUIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FBWF_GUI.h"
#include "FBWF_GUIDlg.h"
#include "fbwfapi/fbwfapi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFBWF_GUIDlg dialog




CFBWF_GUIDlg::CFBWF_GUIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFBWF_GUIDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFBWF_GUIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_MODIFY_FILE_LIST, m_modify_file_list);
	DDX_Control(pDX, IDC_PROGRESS_RAM_USAGE, m_ram_usage);
	DDX_Control(pDX, IDC_STATIC_RAM_INFO, m_driver_information);
	DDX_Control(pDX, IDC_LIST_LOG, m_log);
}

BEGIN_MESSAGE_MAP(CFBWF_GUIDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_COMMIT, &CFBWF_GUIDlg::OnBnClickedButtonCommit)
	ON_BN_CLICKED(IDC_BUTTON_SELECTALL, &CFBWF_GUIDlg::OnBnClickedButtonSelectall)
	ON_BN_CLICKED(IDC_BUTTON_UNSELECTALL, &CFBWF_GUIDlg::OnBnClickedButtonUnselectall)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CFBWF_GUIDlg::OnBnClickedButtonRefresh)
END_MESSAGE_MAP()


// CFBWF_GUIDlg message handlers

BOOL CFBWF_GUIDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	CString stringBuffer;
	pVolumeString = L"C:";

	CRect rect;
	m_modify_file_list.GetClientRect(&rect);
	m_modify_file_list.InsertColumn(0,L"id",	0,	int(rect.Width() * 0.08));
	m_modify_file_list.InsertColumn(1,L"open",	0,	int(rect.Width() * 0.05));
	m_modify_file_list.InsertColumn(2,L"size",	LVCFMT_RIGHT,	int(rect.Width() * 0.10));
	m_modify_file_list.InsertColumn(3,L"file path",0,int(rect.Width() * 0.75));

	m_modify_file_list.SetExtendedStyle(
		m_modify_file_list.GetExtendedStyle()|
		LVS_EX_FULLROWSELECT |
		LVS_EX_GRIDLINES |
		LVS_EX_CHECKBOXES
	);

	//Adding source below.
	UpdateUsage();
	GetModifyFiles();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFBWF_GUIDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFBWF_GUIDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CFBWF_GUIDlg::UpdateUsage()
{
	CString stringBuffer;
	FbwfMemoryUsage memoryUsage;
	m_ram_usage.SetRange(0,100);

	if (FbwfGetMemoryUsage(&memoryUsage) == NO_ERROR)
	{
		m_ram_usage.SetPos(
			int(100.0 * (memoryUsage.dirStructure + memoryUsage.fileData) 
			/ memoryUsage.currCacheThreshold));


		stringBuffer.Format(L"FBWF Driver using %d Mb of %d Mb.",
			(memoryUsage.dirStructure + memoryUsage.fileData) / 1024 /1024,
			(memoryUsage.currCacheThreshold) / 1024 /1024);
		m_driver_information.SetWindowText(stringBuffer);
	}
	else
	{
		m_ram_usage.SetPos(0);
	}

	//stringBuffer.Format(L"Default volume is %s", this->pVolumeString);
	//LogString(stringBuffer);
}

void CFBWF_GUIDlg::GetModifyFiles()
{
	//Adding source below.
	ULONG ret;
	PFbwfCacheDetail pcd = NULL;
	ULONG cd_size = sizeof(FbwfCacheDetail) + sizeof(wchar_t) * 255;
	ULONG size = cd_size;

	ULONG currentSession = 0;
	ULONG nextSession = 0;

	int id = 0;
	CString tmpStringBuffer;

	m_modify_file_list.DeleteAllItems();
	listModifyFileList.clear();

	wchar_t VolumeString[3] = L"C:";

	pcd = (PFbwfCacheDetail)malloc(cd_size);

	for (int i = 'a';i <= 'z';i++)
	{
		VolumeString[0] = i;
		if (FbwfIsVolumeProtected(VolumeString, &currentSession, &nextSession)
			== NO_ERROR)
		{
			if (currentSession)
			{
				memset(pcd, 0, cd_size);
				ret = FbwfFindFirst(VolumeString,
					pcd,
					&size);

				if (ret == NO_ERROR)
				{
					do{
						//Save data to listctrl
						tmpStringBuffer.Format(L"%d", id);
						m_modify_file_list.InsertItem(id,tmpStringBuffer);

						tmpStringBuffer.Format(L"%d", pcd->openHandleCount);
						m_modify_file_list.SetItemText(id,1,tmpStringBuffer);

						tmpStringBuffer.Format(L"%d Kb", pcd->cacheSize / 1024);
						m_modify_file_list.SetItemText(id,2,tmpStringBuffer);

						tmpStringBuffer.Format(L"%s%s",VolumeString,pcd->fileName);
						m_modify_file_list.SetItemText(id,3,tmpStringBuffer);

						listModifyFileList.push_back(pcd->fileName);

						//Prepare next data.
						id++;
						size = cd_size;
						memset(pcd, 0, cd_size);
						ret = FbwfFindNext(pcd, &size);
					}while(ret == ERROR_SUCCESS);

					if ((ret != ERROR_SUCCESS) && (ret != ERROR_NO_MORE_FILES))
					{
						//printf("return value %d\n", ret);
					}
				}
				else
				{
					//printf("return value %d\n", ret);
				}

				FbwfFindClose();
			}
		}
		else
		{
			//printf("Current is not protected!\n");
			//return -1;
		}
	}

	free(pcd);
	pcd = NULL;
}
void CFBWF_GUIDlg::OnBnClickedButtonCommit()
{
	// TODO: Add your control notification handler code here
	int i = 0;
	CString modifyFilePath;
	CString tmpStringBuffer;

	for (i = 0;i < m_modify_file_list.GetItemCount();i++)
	{
		modifyFilePath = listModifyFileList[i];

		if (m_modify_file_list.GetCheck(i))
		{
			if (FbwfCommitFile(
				pVolumeString,
				modifyFilePath.GetBuffer()) == NO_ERROR)
			{
				modifyFilePath.ReleaseBuffer();

				tmpStringBuffer.Format(L"Commit PASS : %s%s", 
					pVolumeString,
					modifyFilePath);
				LogString(tmpStringBuffer);
			}
			else
			{
				tmpStringBuffer.Format(L"Commit FAIL : %s%s", 
					pVolumeString,
					modifyFilePath);
				LogString(tmpStringBuffer);
			}			
		}
		else
		{
		}
	}
}

void CFBWF_GUIDlg::OnBnClickedButtonSelectall()
{
	// TODO: Add your control notification handler code here
	int i = 0;

	for (i = 0;i < m_modify_file_list.GetItemCount();i++)
	{
		m_modify_file_list.SetCheck(i,TRUE);
	}
}

void CFBWF_GUIDlg::OnBnClickedButtonUnselectall()
{
	// TODO: Add your control notification handler code here
	int i = 0;

	for (i = 0;i < m_modify_file_list.GetItemCount();i++)
	{
		m_modify_file_list.SetCheck(i,FALSE);
	}
}

void CFBWF_GUIDlg::LogString(CString& logstring)
{
	int index = 0;

	index = m_log.GetCount();

	m_log.AddString(logstring);
	m_log.SetCurSel(index);
}
void CFBWF_GUIDlg::OnBnClickedButtonRefresh()
{
	// TODO: Add your control notification handler code here
	UpdateUsage();
	GetModifyFiles();
}
